package com.kmxd.ams.controller.arrange;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.arrange.IBoxSvc;
import com.kmxd.ams.client.arrange.dto.BoxSummaryDTO;
import com.kmxd.ams.client.arrange.dto.DetachDTO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.arrange.entity.Box;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 盒级 前端控制器
 *
 * @author ZuoLG
 * @since 2022-07-20
 */
@RestController
@Tag(name = "盒级")
@RequestMapping("/arrange/box")
@AllArgsConstructor
public class BoxCtl {

  private IBoxSvc boxService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @RequestMapping("/selectPage")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<Box>> selectPage(Page reqPage, Box req) {
    return R.success(boxService.selectPage(reqPage, req), req);
  }

  /** 列表查询 */
  @Operation(summary = "列表查询")
  @RequestMapping("/selectList")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "列表查询")
  public R<List<Box>> selectList(Box req) {
    return R.success(boxService.selectList(req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @SaCheckPermission("arrange:box:add")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody Box req) {
    req.setFondsId(
        ObjectUtil.isEmpty(req.getFondsId()) ? SecurityUtil.getFondsId() : req.getFondsId());
    boxService.add(req);
    return R.success();
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @SaCheckPermission("arrange:box:edit")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody Box req) {
    boxService.edit(req);
    return R.success();
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @SaCheckPermission("arrange:box:remove")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH_RISK, title = "删除")
  public R<Void> remove(@Validated @RequestBody DetachDTO req) {
    boxService.remove(req);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @PostMapping("/view")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "查看")
  public R<Box> view(@Validated @RequestBody Box req) {
    return R.success(boxService.view(req));
  }

  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(Box req, HttpServletResponse response) {
    String filename = "盒级列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(Box.class)
        .sheet("盒级列表")
        .doWrite(boxService.selectList(req));
  }
  /** 汇总 */
  @Operation(summary = "汇总")
  @PostMapping("/summary")
  @SaCheckPermission("arrange:box:summary")
  @AutoLog(module = "盒级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.MEDIUM, title = "汇总")
  public R<Void> summary(@Validated @RequestBody BoxSummaryDTO req) {
    boxService.summary(req);
    return R.success();
  }
}
